package com.appspot.gaeforum309.db;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.Query;

import com.appspot.gaeforum309.core.ForumAPI;
import com.appspot.gaeforum309.pathing.Action;
import com.appspot.gaeforum309.pathing.PathableClass;
import com.appspot.gaeforum309.pathing.Action.ActionType;
import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class DBTopic implements IPathable {
	public static final String tagName = "Topic";
	
	public static final PathableClass pathable = new PathableClass(tagName, DBCategory.tagName){

		@Override
		public IPathable getObject(String value) {
			return DBTopic.getTopic(value);
		}
	};
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	@Persistent
	private String title;
	
	@Persistent
	private String description;

	@Persistent
	private Key category;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Key getKey() {
		return key;
	}
	
	@Override
	public String getKeyStr()
	{
		return KeyFactory.keyToString(getKey());
	}
	
	@Override
	public String getParentKeyStr() {
		return KeyFactory.keyToString(category); 
	}

	public DBTopic(String title, String description, Key category) {
		super();
		this.title = title;
		this.description = description;
		this.category = category;
	}
	
	public static DBTopic createTopic(DBCategory cat, String title, String description)
	{
		DBTopic topic = new DBTopic(title, description, cat.getKey());
		
		PMF.pm().makePersistent(topic);
		
		return topic;
	}
	
	public static List<DBTopic> getAllTopicsOnCategory(DBCategory cat)
	{
		Query q = PMF.pm().newQuery(DBTopic.class);	
		q.setFilter("category == argCategory");
		q.declareParameters("com.google.appengine.api.datastore.Key argCategory");
		
		@SuppressWarnings("unchecked")
		List<DBTopic> res = (List<DBTopic>)q.execute(cat.getKey());
		
		return res;
	}
	
	public static DBTopic getTopic(String keystr)
	{
		Key key = KeyFactory.stringToKey(keystr);
		if(key == null)
			return null;
		
		DBTopic res = (DBTopic)PMF.pm().getObjectById(DBTopic.class, key);
		return res;
	}

	@Override
	public String label() {
		return getTitle();
	}
	
	@Override
	public PathableClass getPathable() {
		return pathable;
	}

	@Override
	public List<? extends IPathable> getChilds(int fromNumber, int toNumber) {
		return DBConversation.getConversationsWithTopic(this, fromNumber, toNumber);
	}

	@Override
	public List<? extends IPathable> getChilds() {
		return DBConversation.getAllConversationWithTopic(this);
	}

	@Override
	public boolean authorizedAction(ActionType actionType) {
		switch(actionType)
		{
		case createChild:
			if(ForumAPI.loggedUser())
				return true; // Only admin can create new topic
			else
				return false;		
		}
		return false;
	}

	@Override
	public boolean applyAction(Action action) {
		String title = action.getField("Title").defaultValue;
		String description = action.getField("Description").defaultValue;
		String comment = action.getField("Comment").defaultValue;
		
		DBUser user = ForumAPI.userLogin();
		
		DBConversation dbconversation = DBConversation.createConversation(this, title, description);
		DBComment dbcomment = DBComment.createComment(dbconversation, comment, user);
		if(dbcomment == null || dbconversation == null)
			return false; // TODO: If comment not created, remove conversation !
		
		return true;
	}
}
